<?php
/**
 *   Joomdiet - administrator/components/models/recipecategories.php
 *   Author: Juan Dapena Paz (juan@bittingbits.com)
 *   Copyright (C) 2010 Bitting Bits S.L.
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.model');
class JoomdietModelRecipeCategories extends JModel
{
    var $_data = array();
    
    // default query constructor
    function _buildQuery() {
        $query = 'SELECT * FROM #__joomdiet_Recipes_Category ORDER BY parent_category';
        return $query;
    }
    
    function getData()
    {
        if (empty($_data)) {
            $query = $this->_buildQuery();
            $elements = $this->_getList($query);
        }
        
        foreach ($elements as $element) {
            $node = $this->_createNode($element);
            $this->_insertNode($this->_data, $element->parent_category, $node);
        }
        
        return $this->_data;
    }
    
    function _createNode($element)
    {
        $node = array(
            'data' => $element,
            'children' => array()
        );
        
        return $node;
    }
    
    function _insertNode(&$root, $parent, $node)
    {
        $found = false;
        if ($node['data']->parent_category == '') {
            $this->_data[] = $node;
        } else {
            foreach ($root as &$n) {
                if ($n['data']->id == $parent) {
                    array_push($n['children'], $node);
                } else {
                    if ($this->isLeaf($n)) {
                        // do nothing, recursive break condition
                    } else $this->_insertNode($n['children'], $parent, $node);
                }
            }
        }
    }
    
    function isLeaf($node)
    {
        return empty($node['children']);
    }
    
    function deleteNodeByIds($id) {
        $db =& JFactory::getDBO();
        
        try {
            // searching for children
            $query = 'SELECT id FROM #__joomdiet_Recipes_Category'
                .' WHERE parent_category='.$id;
            $db->setQuery($query);
            $db->query();
            $ids = $db->loadResultArray();
            if (is_null($ids)) {
                echo $db->getErrorMsg();
                return;
            }
            
            // deleting children
            foreach ($ids as $child_id) {
                $this->deleteNodeByIds($child_id);
            }
            
            // deleting element
            $query = 'DELETE FROM #__joomdiet_Recipes_Category'
                .' WHERE id='.$id;
            $db->setQuery($query);
            $db->query();
        } catch (Exception $e) {
            // do nothing, it does not matter
        }
    }
    
    function delete($id) {
    }
}
?>
